﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace trimmer
{

    class Processor
    {

     //выбирает все слова из одной категории

        public void Process()
        {
            Console.WriteLine("grouper 0.1");
            DateTime dt_start = DateTime.Now;
            
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };


            foreach (FileInfo fi in Files)
            {
                ProcessFile(fi);
            }
            //global results
           
            
        }



        void ProcessFile(FileInfo fi)
        {
            Console.WriteLine("\nProcessing " + fi.Name);
            
            TextWriter wr = new StreamWriter("./out/" + fi.Name);
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
          
            try
            {
                string line = null;
                System.IO.TextReader readFile = new StreamReader(fi.FullName);
                while ((line = readFile.ReadLine())!=null)
                {
                    //find first 
                    string[] tokens = line.Split('\t');
                    string sem = tokens[0];
                    string rhs = "";
                    try
                    { rhs = tokens[1]; }
                    catch { };
                    string[] rhs_tokens = rhs.Trim().Split(' ');
                    //insert new values in dic
                    if (!dic.ContainsKey(sem)) dic.Add(sem, new List<string>());
                  //    Console.WriteLine("sem = "+sem);
                    for (int i = 0; i < rhs_tokens.Count(); i++)
                    {
                      //  Console.WriteLine("tok = " + rhs_tokens[i]);
                        if (!dic[sem].Contains(rhs_tokens[i]))
                            dic[sem].Add(rhs_tokens[i]);

                    }
                      //  Console.WriteLine(sem);
                }
                readFile.Close();
                readFile = null;
                IEnumerable<KeyValuePair<string, List<string>>> queue;


                queue = dic.OrderBy(pair => pair.Key);
                
                foreach (KeyValuePair<string, List<string>> kvp in queue)
                {
                    wr.Write(kvp.Key+"\t");
                    kvp.Value.Sort();
                    for (int j = 0; j < kvp.Value.Count; j++)
                        wr.Write(kvp.Value[j]+" ");
                    wr.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        

            
            
            
            wr.Close();
            // remove all b,

        }// end processfile
    }

}
